using System;
using System.Collections;
using System.Data.SqlClient;
using System.Web.Services;
using System.Reflection;
using System.Security;
using System.Xml;
using System.Xml.Xsl;

/*	-----------------------------------------------------------------------	
	Copyright:	umlungu consulting (pty) ltd
	Author:		Alan Benington	
	Started:	2005-09-08
	Status:		release
	Version:	2.0.0
	Build:		20070709
	License:	GNU General Public License
	-----------------------------------------------------------------------	*/

/*	-----------------------------------------------------------------------	
	Development Notes:
	==================
	20070709:	Starting point. This object has been created to give access
				via a web service to some of the site internals (eg site and
				user profiles)
	20071102:	Extend functionality with the addition of logging:
				- see x_logger notes for requirements
				- Add* methods (ie public methods do logging)
	20071109:	Further generalise this to produce a more flexible format
				- 'Custom' type
	20071122:	Ensure 'webservice' type is default for backward compat.
	20071123:	x_exception into separate file
	-----------------------------------------------------------------------	*/

namespace umlungu.engineX {
	/// <summary>
	/// umlungu EngineX class. 
	/// class for the EngineX result xml.
	/// </summary>
	[WebService(Namespace="http://www.umlungu.com/engineX/")]
	public class x_result : System.Web.Services.WebService {
		#region Invisible properties
		private string formatstr;
		private string codename;
		private string descname;
		private string rsltname;
		#endregion

		#region Constants
		private const bool _debug = true;

		private const string CODE_WEBSERVICE = "Result_Code";
		private const string CODE_OTHER = "ResultCode";
		private const string CODE_ORIGINAL = "Code";

		private const string FORMAT_WEBSERVICE = "<{0}><Result><Result_Code>0</Result_Code><Description/>{1}</Result></{0}>";
		private const string FORMAT_OTHER = "<{0}><Result><ResultCode>0</ResultCode><Description/>{1}</Result></{0}>";
		private const string FORMAT_ORIGINAL = "<{0}><Result><Code>0</Code><Description/>{1}</Result></{0}>";
		#endregion

		#region Default constants
		private const string DEFAULT_TYPE = "webservice";	// for backward compatibility
		private const string DEFAULT_DESC = "Description";
		private const string DEFAULT_RESULT = "Result";
		#endregion

		#region Visible properties
		/// <summary>Debug flag</summary>
		/// <value>Constant. Set when debugging</value>
		public bool Debug {
			get { return _debug; }
		}
		private string name;
		/// <summary>The result name</summary>
		/// <value>The name of the root node in the result document</value>
		public string Name {
			get { return name; }
			set {	name = (value != "")? value : "x_result"; }
		}
		private XmlDocument result;
		/// <summary>The result document</summary>
		/// <value>The result</value>
		public XmlDocument Result	{
			get	{	return result;	}	
			set	{	result = value;	}
		}
		/// <summary>Start timing mark</summary>
		/// <value>Date-Time of execution start</value>
		protected DateTime starttime;
		private x_resultType resultType = x_resultType.original;
		/// <summary>The result name</summary>
		/// <value>The name of the root node in the result document</value>
		public x_resultType ResultType {
			get { return resultType; }
			set { resultType = value; setResult(); }
		}
		/// <summary>The description of the result</summary>
		/// <value>String describing the result eg "Ok" or "This was an error..."</value>
		public string Description {
			get { return result.SelectSingleNode(String.Concat("//", descname)).InnerText; }
			set { result.SelectSingleNode(String.Concat("//", descname)).InnerText = value; }
		}
		/// <summary>The code identifier of the result</summary>
		/// <value>String defining the result eg "0" or "error_something"</value>
		public string Code {
			get { return result.SelectSingleNode(String.Concat("//", codename)).InnerText; }
			set { result.SelectSingleNode(String.Concat("//", codename)).InnerText = value; }
		}
		/// <summary>The result element</summary>
		/// <value>The xml element that contains the code and the description of the result</value>
		public XmlElement ResultElement {
			get { return result.SelectSingleNode(String.Concat("//", rsltname)) as XmlElement; }	
		}

		private x_logger logger;
		/// <summary>The logger object</summary>
		/// <value>A log4net object</value>
		public x_logger Logger {
			get { return logger; }
		}
		#endregion

		
		#region Constructors/Destructors
		/// <overloads>Constructor</overloads>
		/// <summary>Default constructor</summary>
		public x_result() {
			initialise();
			setType();
		}
		/// <summary>Constructor</summary>
		/// <param name="RootName">Xml root node name</param>
		public x_result(string RootName) {
			initialise(RootName);
			setType();
		}
		/// <summary>Constructor</summary>
		/// <param name="RootName">Xml root node name</param>
		/// <param name="Logtype">The type for logging</param>
		public x_result(string RootName, Type Logtype) {
			initialise(RootName, Logtype);
			setType();
		}
		/// <summary>Constructor</summary>
		/// <param name="Type">The type of result</param>
		/// <param name="RootName">Xml root node name</param>
		/// <param name="Logtype">The type for logging</param>
		public x_result(string Type, string RootName, Type Logtype) {
			initialise(RootName, Logtype);
			setType(Type);
		}
		/// <summary>Constructor</summary>
		/// <param name="Type">The type of result</param>
		/// <param name="RootName">Xml root node name</param>
		public x_result(string Type, string RootName) {
			initialise(RootName);
			setType(Type);
		}
		/// <summary>Custom constructor</summary>
		/// <param name="Type">The type of result</param>
		/// <param name="RootName">Xml root node name</param>
		/// <param name="CodeName">The prefix string for the result</param>
		/// <param name="DescriptionName">The suffix string for the result</param>
		public x_result(string Format, string RootName, string ResultName, string CodeName, string DescriptionName) {
			initialise(RootName);
			formatstr = Format;
			codename = CodeName;
			descname = DescriptionName;
			rsltname = ResultName;
			setType("custom");
		}
		/// <summary>Custom + type constructor</summary>
		/// <param name="Logtype">The type for logging</param>
		/// <param name="RootName">Xml root node name</param>
		/// <param name="CodeName">The prefix string for the result</param>
		/// <param name="DescriptionName">The suffix string for the result</param>
		public x_result(Type Logtype, string Format, string RootName, string ResultName, string CodeName, string DescriptionName) {
			initialise(RootName, Logtype);
			formatstr = Format;
			codename = CodeName;
			descname = DescriptionName;
			rsltname = ResultName;
			setType("custom");
		}
		#endregion


		#region Public web service methods
		/// <summary>Tests the web service</summary>
		/// <returns>Returns an XmlDocument</returns>
		[WebMethod(Description = "Basic test of the web service")]
		public virtual XmlDocument Test() {
			try {
				AddOk();
				return Result;
			}
			catch (System.Exception e) { _AddError(e); return (Result); }
		}
		/// <summary>Tests the web service</summary>
		/// <returns>Returns an the Result name</returns>
		[WebMethod(Description = "Tests the web service by returning the Result name")]
		public virtual string TestName() {
			try {
				return Name;
			} catch (System.Exception e) {
				return e.Message; 
			}
		}
		/// <summary>Tests the custom result formatting</summary>
		/// <returns>Returns a custom Result</returns>
		[WebMethod(Description = "Tests the custom result formatting")]
		public virtual XmlDocument TestCustom(string Format, string RootName, string ResultName, string CodeName, string DescriptionName) {
			try {
				_Custom(Format, RootName, ResultName, CodeName, DescriptionName);
				AddOk();
				return Result;
			} catch (System.Exception e) { _AddError(e); return (Result); }
		}
		/// <summary>Tests the web service</summary>
		/// <returns>Returns an XmlDocument</returns>
		[WebMethod(Description = "Basic test of an error condition")]
		public virtual XmlDocument TestError() {
			AddError();
			return Result;
		}
		/// <summary>Tests the web service exception handling</summary>
		/// <returns>Returns an XmlDocument</returns>
		[WebMethod(Description = "Basic test of an error exception")]
		public virtual XmlDocument TestException() {
			try {
				throw new x_exception("test_exception", "Result exception throw");
			} catch (x_exception e) { 
				_AddError(e); 
			}
			return (Result); 
		}
		#endregion


		#region Public methods
		/// <summary>Add OK result</summary>
		public XmlDocument AddOk() { addSuccess(); logResult(); return (result); }
		/// <summary>Add OK result + node</summary>
		public XmlDocument AddOk(string nodename, string nodeval) { addSuccess(nodename, nodeval); logResult(); return (result); }
		/// <summary>Add OK result + node</summary>
		public XmlDocument AddOk(string node) { addSuccess(node); logResult(); return (result); }
		/// <summary>Add OK result + node</summary>
		public XmlDocument AddOk(XmlNode node) { addSuccess(node); logResult(); return (result); }
		/// <summary>Add OK result + node</summary>
		public XmlDocument AddOk(XmlElement node) { addSuccess(node); logResult(); return (result); }
		/// <summary>Add OK result + node</summary>
		public XmlDocument AddOk(XmlDocument node) { addSuccess(node); logResult(); return (result); }

		/// <summary>Add element to result, usually to act as a parent to other nodes to be added</summary>
		/// <param name="name">Name of the node</param>
		/// <param name="text">Text of the node</param>
		public XmlElement AddElem(string name, string text) { return addNode(name, text) as XmlElement; }

		/// <summary>Add node to result</summary>
		/// <param name="name">Name of the node</param>
		/// <param name="text">Text of the node</param>
		public XmlNode AddNode(string name, string text) { return addNode(name, text); }
		/// <summary>Add node(s) to result</summary>
		/// <param name="nodexml">Xml as string</param>
		public XmlNode AddNode(string nodexml) { return addNode(nodexml); }
		/// <summary>Add node(s) to result</summary>
		/// <param name="nodedoc">Xml as xml document</param>
		public XmlNode AddNode(XmlDocument nodedoc) { return addNode(nodedoc.DocumentElement.OuterXml); }
		/// <summary>Add element to result</summary>
		/// <param name="nodeel">Xml as xml element</param>
		public XmlNode AddNode(XmlNode nodeel) { return addNode(nodeel); }
		/// <summary>Add node to element in result</summary>
		/// <param name="parent">The parent node</param>
		/// <param name="name">Name of the node</param>
		/// <param name="text">Text of the node</param>
		public XmlNode AddNode(XmlElement parent, string name, string text) { return parent.AppendChild(addNode(name, text)); }

		/// <summary>Add Error result</summary>
		public XmlDocument AddError() { addFailure("undefined error", "notOK"); return (result); }
		/// <summary>Add Error result</summary>
		/// <param name="except">Exception object</param>
		public XmlDocument AddError(Exception except) 			{ _AddError(except);  return(result); }
		/// <summary>Add Error result</summary>
		/// <param name="except">SecurityException object</param>
		public XmlDocument AddError(SecurityException except)	{ _AddError(except);  return(result); }
		/// <summary>Add Error result</summary>
		/// <param name="except">UnauthorizedAccessException object</param>
		public XmlDocument AddError(UnauthorizedAccessException except)	{ _AddError(except);  return(result); }
		/// <summary>Add Error result</summary>
		/// <param name="except">XsltException object</param>
		public XmlDocument AddError(XsltException except) { _AddError(except); return (result); }
		/// <summary>Add Error result</summary>
		/// <param name="except">x_exception object</param>
		public XmlDocument AddError(x_exception except)			{ _AddError(except);  return(result); }
		/// <summary>Add Error result</summary>
		/// <param name="err">Error string</param>
		public XmlDocument AddError(string err) { _AddError(err); return (result); }
		/// <summary>Add Error result</summary>
		/// <param name="errorcode">Error string</param>
		/// <param name="description">Error string</param>
		public XmlDocument AddError(string errorcode, string description) { _AddError(description, errorcode); return (result); }

		/// <summary>Add time attribute to the result</summary>
		public void AddTime() { addTime(); }
		#endregion


		#region Protected methods
		/// <summary>Set the result to custom</summary>
		protected void _Custom(string Format, string RootName, string ResultName, string CodeName, string DescriptionName) {
			initialise(RootName);
			formatstr = Format;
			codename = CodeName;
			descname = DescriptionName;
			rsltname = ResultName;
			setType("custom");
		}


		/// <overloads>Assigns the OK result. (code = "0", text = "OK")</overloads>
		/// <summary>
		/// Standard OK result.
		/// </summary>
		protected void _AddOk() {
			addSuccess();
		}
		/// <summary>OK result with an additional result node</summary>
		/// <param name="nodename">Name of the node</param>
		/// <param name="nodeval">Text value of the node</param>
		protected void _AddOk(string nodename, string nodeval) {
			addSuccess(nodename, nodeval);
		}
		/// <summary>OK result with an additional result node</summary>
		/// <param name="node">Xml node object</param>
		protected void _AddOk(XmlNode node) {
			addSuccess(node);
		}
		/// <summary>OK result with an additional result element</summary>
		/// <param name="node">Xml element object</param>
		protected void _AddOk(XmlElement node) {
			addSuccess(node);
		}
		/// <summary>OK result with an additional result node</summary>
		/// <param name="node">Xml node string</param>
		protected void _AddOk(string node) {
			addSuccess(node);
		}
		
		/// <overloads>Assigns the error result.</overloads>
		/// <summary>
		/// Standard error result (text = "notOK")
		/// </summary>
		/// <param name="what">Error code</param>
		protected void _AddError(string what) {
			addFailure("notOK", what);
		}
		/// <summary>
		/// Specific error result
		/// </summary>
		/// <param name="which">Error description</param>
		/// <param name="what">Error code</param>
		protected void _AddError(string which, string what) {
			addFailure(which, what);
		}
		/// <summary>
		/// Exception error result
		/// </summary>
		/// <param name="except">x_exception error</param>
		protected void _AddError(x_exception except) {
			addFailure(except.Message, except.Code);
			if (_debug)
				((XmlElement)addNode("Result_Trace", except.StackTrace)).SetAttribute("type", "x_exception");
		}
		/// <summary>
		/// Exception error result
		/// </summary>
		/// <param name="except">Exception error</param>
		protected void _AddError(Exception except) {
			addFailure(except.Message, "notOK");
			if (_debug)
				((XmlElement)addNode("Result_Trace", except.StackTrace)).SetAttribute("type", "Exception");
		}
		/// <summary>
		/// Exception error result
		/// </summary>
		/// <param name="except">SecurityException error</param>
		protected void _AddError(SecurityException except) {
			addFailure(String.Concat(except.Message, " -- state:", except.PermissionState), "notOK");
			if (_debug)
				((XmlElement)addNode("Result_Trace", except.StackTrace)).SetAttribute("type", "SecurityException");
		}
		/// <summary>
		/// Exception error result
		/// </summary>
		/// <param name="except">SqlException error</param>
		protected void _AddError(SqlException except) {
			addFailure(String.Concat(except.Message, " -- server:", except.Server, " -- state:", except.State, " -- source:", except.Source, " -- proc:", except.Procedure, " -- line:", except.LineNumber), "notOK");
			if (_debug)
				((XmlElement)addNode("Result_Trace", except.StackTrace)).SetAttribute("type", "SqlException");
		}
		/// <summary>
		/// Exception error result
		/// </summary>
		/// <param name="except">UnauthorizedAccessException error</param>
		protected void _AddError(UnauthorizedAccessException except) {
			addFailure(except.Message, "notOK");
			if (_debug)
				((XmlElement)addNode("Result_Trace", except.StackTrace)).SetAttribute("type", "UnauthorizedAccessException");
		}
		/// <summary>
		/// Exception error result
		/// </summary>
		/// <param name="except">UnauthorizedAccessException error</param>
		protected void _AddError(XsltException except) {
			addFailure(except.Message, "notOK");
			addNode("Source", except.SourceUri);
			addNode("Line", except.LineNumber.ToString());
			addNode("Char", except.LinePosition.ToString());
		}

		/// <overloads>Adds to the result document.</overloads>
		/// <summary>
		/// Adds a node to the result.
		/// </summary>
		/// <param name="name">Name of the node</param>
		/// <param name="value">Value of the node</param>
		protected void _Add(string name, string value) {
			addNode(name, value);
		}
		/// <summary>
		/// Adds a node with attribute to the result.
		/// </summary>
		/// <param name="name">Name of the node</param>
		/// <param name="value">Value of the node</param>
		/// <param name="atname">Name of the attribute</param>
		/// <param name="atval">Value of the attribute</param>
		protected void _Add(string name, string value, string atname, string atval) {
			addNode(name, value, atname, atval);
		}
		/// <summary>
		/// Adds a CDATA node to the result.
		/// </summary>
		/// <param name="name">Name of the node</param>
		/// <param name="value">Value of the node</param>
		protected void _AddText(string name, string value) {
			addCData(name, value);
		}
		#endregion
		
		#region Private methods
		/// <summary>addSuccess::Overloaded method(s) for setting a successful result</summary>
		/// <returns>nothing.</returns>
		private void addSuccess() {
			//Result.LoadXml(String.Concat(openstr, closestr));
			Result.LoadXml(String.Format(formatstr, name, ""));
			Description = "OK";
		}
		/// <param name="thisnode">Xml Node</param>
		private void addSuccess(XmlElement thisnode) {
			//Result.InnerXml = String.Concat(openstr, thisnode.OuterXml, closestr);
			Result.InnerXml = String.Format(formatstr, name, thisnode.OuterXml);
			Description = "OK";
		}
		/// <param name="thisnode">Xml Node</param>
		private void addSuccess(XmlNode thisnode) {
			//Result.InnerXml = String.Concat(openstr, thisnode.InnerXml, closestr);
			Result.InnerXml = String.Format(formatstr, name, thisnode.InnerXml);
			Description = "OK";
		}
		/// <param name="nodexml">Xml Node string</param>
		private void addSuccess(string nodexml) {
			//Result.InnerXml = String.Concat(openstr, nodexml, closestr);
			Result.InnerXml = String.Format(formatstr, name, nodexml);
			Description = "OK";
		}
		/// <param name="nodename">Xml Node name</param>
		/// <param name="nodeval">Xml Node value</param>
		private void addSuccess(string nodename, string nodeval) {
			XmlElement newElem = Result.CreateElement(nodename);
       		newElem.InnerText = nodeval;
			//Result.InnerXml = String.Concat(openstr, closestr);
			Result.InnerXml = String.Format(formatstr, name, "");
			ResultElement.AppendChild(newElem);
			Description = "OK";
		}
		/// <overloads>Set a failed result</overloads>
		/// <returns>nothing.</returns>
		/// <summary></summary>
		/// <param name="errdesc">Error description</param>
		/// <param name="errcode">Error code</param>
		private void addFailure(string errdesc, string errcode) {
			//Result.LoadXml(String.Concat(openstr, closestr));
			Result.LoadXml(String.Format(formatstr, name, ""));
			Code = errcode;
			Description = errdesc;
		}
		/// <summary></summary>
		/// <param name="exc">Error exception</param>
		/// <param name="errcode">Error code</param>
		private void addFailure(Exception exc, string errcode) {
			//Result.LoadXml(String.Concat(openstr, closestr));
			Result.LoadXml(String.Format(formatstr, name, ""));
			Code = errcode;
			Description = String.Concat(name, " error::", exc.Source, ":-:", exc.Message, "::[", exc.StackTrace, "]");
		}
		/// <summary></summary>
		/// <param name="exc">Error exception</param>
		/// <param name="errcode">Error code</param>
		private void addFailure(Exception exc, int errcode) {
			addFailure(exc, errcode.ToString());
		}
		/// <summary></summary>
		/// <param name="errdesc">Error description</param>
		/// <param name="errcode">Error code</param>
		private void addFailure(string errdesc, int errcode) {
			addFailure(errdesc, errcode.ToString());
		}
		/// <overloads>Add nodes to the result xml</overloads>
		/// <returns>nothing.</returns>
		/// <summary>Node as Xml string</summary>
		/// <param name="nodexml">Xml Node string</param>
		public XmlNode addNode(string nodexml) {
			XmlNode thisnode = ResultElement;
			thisnode.InnerXml = String.Concat(thisnode.InnerXml, nodexml);
			return(thisnode);
		}
		/// <summary>Node as Xml node</summary>
		/// <param name="thisnode">Xml Node</param>
		public XmlNode addNode(XmlNode thisnode) {
			if (thisnode != null) {	// cannot import null node
				XmlNode newnode = Result.ImportNode(thisnode, true);
				XmlNode resnode = ResultElement;
				resnode.AppendChild(newnode);
				return(resnode);
			}
			else
				return null;
		}
		/// <summary>Add node as name and text value</summary>
		/// <param name="nodename">Xml Node name</param>
		/// <param name="nodeval">Xml Node value</param>
		public XmlNode addNode(string nodename, string nodeval) {
			XmlElement newElem = Result.CreateElement(nodename);
       		newElem.InnerText = nodeval;
			XmlNode resnode = ResultElement;
			resnode.AppendChild(newElem);
			return(newElem);
		}
		/// <summary>Add node with named attribute</summary>
		/// <param name="nodename">Name of the node</param>
		/// <param name="nodeval">Value of the node</param>
		/// <param name="attrname">Name of the attribute</param>
		/// <param name="attrval">Value of the attribute</param>
		public XmlNode addNode(string nodename, string nodeval, string attrname, string attrval) {
			XmlElement newElem = Result.CreateElement(nodename);
       		newElem.InnerText = nodeval;
			newElem.SetAttribute(attrname, attrval);
			XmlNode resnode = ResultElement;
			resnode.AppendChild(newElem);
			return(newElem);
		}
		/// <summary>Add multiple nodes to the result xml</summary>
		/// <param name="thisnode">Xml Node containing nodes</param>
		public XmlNode addNodes(XmlNode thisnode) {
			XmlNode newnode;
			if (thisnode != null) {	// cannot import null node
				XmlNode resnode = ResultElement;
				foreach (XmlNode childnode in thisnode.ChildNodes) {
					newnode = Result.ImportNode(childnode, true);
					resnode.AppendChild(newnode);
					}
				return(resnode);
			}
			else
				return null;
		}
		/// <param name="nodename">Name of the node</param>
		/// <param name="nodeval">Value of the node</param>
		private void addAttr(string nodename, string nodeval) {
			XmlElement thiselem = (XmlElement)ResultElement;
			thiselem.SetAttribute(nodename, nodeval);
		}
		/// <overloads>Set a time value to the result</overloads>
		/// <summary>
		/// Time from start to now
		/// </summary>
		/// <returns>nothing.</returns>
		private void addTime() {
			DateTime endtime = DateTime.Now;
			TimeSpan timetaken = endtime - starttime;
			string thistime = timetaken.TotalSeconds.ToString();
			XmlElement thiselem = (XmlElement)ResultElement;
			thiselem.SetAttribute("time", thistime);
		}
		/// <summary>
		/// Time from start to now with mark
		/// </summary>
		/// <param name="mark">description of the mark point</param>
		private void addTime(string mark) {
			DateTime endtime = DateTime.Now;
			TimeSpan timetaken = endtime - starttime;
			string thistime = timetaken.TotalSeconds.ToString();
			addNode(mark, thistime);
		}
		/// <summary>
		/// Time from elapsed timespan
		/// </summary>
		/// <param name="time">elapsed time value</param>
		private void addTime(TimeSpan time) {
			string thistime = time.TotalSeconds.ToString();
			XmlElement thiselem = (XmlElement)ResultElement;
			thiselem.SetAttribute("time", thistime);
		}
		/// <summary>addCData::Overloaded method(s) for adding data to the result</summary>
		/// <param name="nodename">Name of the node</param>
		/// <param name="cdatastr">string containing the data</param>
		/// <returns>nothing.</returns>
		private void addCData(string nodename, string cdatastr) {
			XmlElement newElem = Result.CreateElement(nodename);
			XmlCDataSection CData;
			CData = Result.CreateCDataSection(cdatastr);    
			newElem.AppendChild(CData);  
			// now append the node
			XmlNode resnode = ResultElement;
			resnode.AppendChild(newElem);
		}
		#endregion

		#region General private methods
		/// <overloads>Initialises the object (called form the constructor)</overloads>
		/// <summary>Default</summary>
		private void initialise() {
			initialise("", typeof(x_result));
		}
		/// <summary>Default</summary>
		private void initialise(string name) {
			initialise(name, typeof(x_result));
		}
		/// <summary>Default</summary>
		private void initialise(string name, Type type) {
			starttime = DateTime.Now;
			Name = name;
			Result = new XmlDocument();
			if (type != null)
				logger = new x_logger(type);
		}
		/// <summary>Sets the result type from a string</summary>
		private void setResult() {
			switch (resultType) {
				case x_resultType.webservice:
					formatstr = FORMAT_WEBSERVICE;
					codename = CODE_WEBSERVICE;
					descname = DEFAULT_DESC;
					rsltname = DEFAULT_RESULT;
					break;
				case x_resultType.original:
					formatstr = FORMAT_ORIGINAL;
					codename = CODE_ORIGINAL;
					descname = DEFAULT_DESC;
					rsltname = DEFAULT_RESULT;
					break;
				case x_resultType.custom:
					// formatstr, descname and codename set in constructor
					break;
				case x_resultType.other:
				default:
					formatstr = FORMAT_OTHER;
					codename = CODE_OTHER;
					descname = DEFAULT_DESC;
					rsltname = DEFAULT_RESULT;
					break;
			}
		}
		/// <summary>Sets the result type from a string</summary>
		private void setType() {
			setType(DEFAULT_TYPE);
		}
		/// <summary>Sets the result type from a string</summary>
		private void setType(string type) {
			switch (type) {
				case "webservice":
					resultType = x_resultType.webservice;
					break;
				case "original":
					resultType = x_resultType.original;
					break;
				case "custom":
					resultType = x_resultType.custom;
					break;
				case "x":
				case "X":
				default:
					resultType = x_resultType.other;
					break;
			}
			setResult();
		}
		/// <summary>logs the result document</summary>
		private void logResult() {
			if (logger == null)
				return;
			else
				logger.Info(Result);
		}
		#endregion
	}
}
